<template>
  <div class="component">
    <form>
      <div class="form-group">
        <textarea class="form-textarea"
          rows="14"
          placeholder="JSON格式字符串"
          v-model="param"
        ></textarea>
      </div>
      <div class="text-left">
        <a class="text-link text-success" @click="handleFormat">
          <i class="mr-xxs icon icon-format"></i>
          格式化
        </a>
        <span v-if="isShowStatic" class="form-checkbox">
          <label><input name="checkbox" type="checkbox" v-model="isEnableStatic">是否存储为静态数据</label>
        </span>
      </div>
    </form>
  </div>
</template>

<script>
/* 导入参数 */
import { Messager } from '@daelui/vdog/dist/components'

export default {
  props: {
    isShowStatic: {
      type: Boolean,
      default () {
        return false
      }
    }
  },

  data () {
    return {
      param: '{}',
      isEnableStatic: false
    }
  },

  methods: {
    /**
     * @function 载入数据
    */
    load () {
      this.param = '{}'
    },
    /**
     * @function 格式化处理
    */
    handleFormat () {
      let param = ''
      try {
        param = (new Function('return ' + this.param))()
      } catch (e) {
        Messager.warning('数据非JSON格式，格式化失败')
      }

      // 对象形式格式化
      if (typeof param === 'object') {
        this.param = JSON.stringify(param, null, 2)
      }
    },
    /**
     * @function 获取状态
     * @return {Object}
    */
    getState () {
      let data = this.param
      let params = ''
      try {
        params = (new Function('return ' + data))()
      } catch (e) {
        params = ''
        messager.warning('数据非JSON格式，导入失败')
      }

      return params
    }
  }
}
</script>

<style lang="less">

</style>
